//package com.tanchengjin.admin.modules.cms.controller;
//
//import com.github.pagehelper.PageHelper;
//import com.tanchengjin.admin.aop.needPermission.NeedPermission;
//import com.tanchengjin.admin.aop.needRole.NeedRole;
//import com.tanchengjin.admin.aop.operationLog.OperationLog;
//import com.tanchengjin.admin.modules.cms.enumeration.ArticleStatus;
//import com.tanchengjin.admin.modules.cms.mapper.ArticleMapper;
//import com.tanchengjin.admin.modules.cms.mapper.CategoryMapper;
//import com.tanchengjin.admin.modules.cms.pojo.Article;
//import com.tanchengjin.admin.modules.cms.pojo.ArticleCategory;
//import com.tanchengjin.admin.model.vo.ArticleCategoryVO;
//import com.tanchengjin.admin.model.vo.ArticleVO;
//import com.tanchengjin.admin.service.ArticleCategoryService;
//import com.tanchengjin.admin.service.ArticleService;
//import com.tanchengjin.admin.utils.LayuiAdminServerResponse;
//import com.tanchengjin.admin.utils.ServerResponse;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.ArrayList;
//import java.util.List;
//
//
///**
// * article Controller
// *
// * @author TanChengjin
// * @version v1.0.0
// * @since v1.0.0
// */
//@Controller
//@RequestMapping("${app.admin.prefix}/articleManager")
//public class ArticleController {
//    /**
//     * Service
//     */
//    @Autowired
//    private ArticleService articleService;
//
//    /**
//     * Mapper
//     */
//    @Autowired
//    private CategoryMapper categoryMapper;
//
//    @Autowired
//    private ArticleMapper articleMapper;
//
//    @Autowired
//    private ArticleCategoryService articleCategoryService;
//
//
//    @GetMapping("/index")
//    public String index() {
//        return "/admin/article/list";
//    }
//
//
//    @RequestMapping(value = "/list", method = {RequestMethod.GET})
//    @ResponseBody
//    public LayuiAdminServerResponse index(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit, Article article) {
//        PageHelper.startPage(page, limit);
//        List<Article> articleList = articleService.getAllByCondition(article);
//        for (Article article1 : articleList) {
//            Integer categoryId = article1.getCategoryId();
//            if (categoryId != null && categoryId != 0) {
//                ArticleCategory category = categoryMapper.selectByPrimaryKey(categoryId);
//                if (category != null) {
//                    article1.setCategoryName(category.getName());
//                }
//            }
//        }
//        long count = articleService.count();
//        return LayuiAdminServerResponse.responseWithSuccess(buildArticleVO(articleList), String.valueOf(count));
//    }
//
//    @RequestMapping(value = "/test", method = {RequestMethod.GET})
//    @ResponseBody
//    public ServerResponse test() {
//        Article article = new Article();
//        article.setTitle("123");
//        article.setAuthor("tan");
//        article.setContent("content");
//        int insert = articleMapper.insert(article);
//
//        return ServerResponse.responseWithSuccess(insert, "success");
//    }
//
//    /**
//     * 将文章实体转换为文章视图实体类
//     *
//     * @param articleList
//     * @return
//     */
//    private List<ArticleVO> buildArticleVO(List<Article> articleList) {
//        List<ArticleVO> articleVOS = copyPropertiesList(articleList);
//        for (ArticleVO articleVO : articleVOS) {
//            if (articleVO.getCategoryId() != null && articleVO.getCategoryId() != 0) {
//                ArticleCategory articleCategory = categoryMapper.selectByPrimaryKey(articleVO.getCategoryId());
//                if (articleCategory != null) {
//                    articleVO.setCategoryName(articleCategory.getName());
//                }
//            }
//        }
//        return articleVOS;
//    }
//
//    //解决BeanUtils.copyProperties无法copy列表
//    private List<ArticleVO> copyPropertiesList(List<Article> articleList) {
//        ArrayList<ArticleVO> articleVOS = new ArrayList<>();
//        for (Article article : articleList) {
//            ArticleVO articleVO = new ArticleVO();
//            BeanUtils.copyProperties(article, articleVO);
//            articleVOS.add(articleVO);
//        }
//        return articleVOS;
//
//    }
//
//    @NeedPermission(value = {"article:add"},responseType = NeedRole.ResponseType.HTML)
//    @RequestMapping(value = "/create", method = {RequestMethod.GET})
//    public String create(Model view) {
//        List<ArticleCategoryVO> tree = articleCategoryService.getTree();
//        view.addAttribute("categoryList", tree);
//        view.addAttribute("articleStatusList", ArticleStatus.values());
//        return "/admin/article/add";
//    }
//
//    /**
//     * <p>
//     * POST http://www.example.com HTTP/1.1
//     * Content-Type: application/json;charset=UTF-8
//     * </p>
//     *
//     * @return ServerResponse
//     */
//    @OperationLog(value = "{}保存文章")
//    @RequestMapping(value = "", method = {RequestMethod.POST})
//    @ResponseBody
//    public ServerResponse store(@RequestBody Article article) {
//        int i = articleService.create(article);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailureMessage("创建失败请重试");
//        }
//    }
//
//    /**
//     * <p>
//     * POST http://www.example.com HTTP/1.1
//     * Content-Type: application/x-www-form-urlencoded;charset=UTF-8
//     * </p>
//     *
//     * @return ServerResponse
//     */
//    @RequestMapping(value = "/submit", method = {RequestMethod.POST})
//    @ResponseBody
//    public ServerResponse submit(Article article) {
//        int i = articleService.create(article);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailureMessage("创建失败请重试");
//        }
//    }
//
//    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
//    @ResponseBody
//    public ServerResponse<Article> show(@PathVariable("id") int id) {
//        Article article = articleService.findOneById(id);
//        return ServerResponse.responseWithSuccess(article);
//    }
//
//    @RequestMapping(value = "/{id}/edit", method = {RequestMethod.GET})
//    public String edit(Model view, @PathVariable(value = "id") int id) {
//        Article article = articleService.findOneById(id);
//        view.addAttribute("article", article);
//        view.addAttribute("articleStatusList", ArticleStatus.values());
//        return "/admin/article/edit";
//    }
//
//    /**
//     * <p>
//     * POST http://www.example.com HTTP/1.1
//     * Content-Type: application/json;charset=UTF-8
//     * </p>
//     *
//     * @return ServerResponse
//     */
//    @OperationLog(value = "{}编辑文章")
//    @RequestMapping(value = "/{id}", method = {RequestMethod.PUT})
//    @ResponseBody
//    public ServerResponse update(@RequestBody Article article, @PathVariable("id") int id) {
//        int i = articleService.updateById(article, id);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailureMessage("更新失败，请重试");
//        }
//    }
//
//    @OperationLog(value = "{}删除文章")
//    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
//    @ResponseBody
//    public ServerResponse destroy(@PathVariable(value = "id") int id) {
//        int i = articleService.deleteById(id);
//
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailureMessage("删除失败请重试");
//        }
//    }
//
//    @OperationLog(value = "{}批量删除文章")
//    @RequestMapping(value = "/batchDelete", method = {RequestMethod.DELETE})
//    @ResponseBody
//    public ServerResponse batchDelete(@RequestParam(value = "ids[]") long[] ids) {
//        int i = articleService.batchDelete(ids);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailureMessage("删除失败请重试");
//        }
//    }
//
//    /**
//     * <p>
//     * POST http://www.example.com HTTP/1.1
//     * Content-Type: application/x-www-form-urlencoded;charset=UTF-8
//     * </p>
//     *
//     * @return ServerResponse
//     */
//    @OperationLog(value = "{}更新文章")
//    @RequestMapping(value = "/{id}/change", method = {RequestMethod.PUT})
//    @ResponseBody
//    public ServerResponse change(Article article, @PathVariable("id") int id) {
//        int i = articleService.updateById(article, id);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailureMessage("更新失败，请重试");
//        }
//    }
//}